<template>
    <a-modal :title="formType == 0 ? '编辑' : '添加'" :width="1000" v-model:visible="visible" @ok="onSubmit">
        <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 7 }">
            <a-tabs v-model:activeKey="activeKey">
                <a-tab-pane v-for="(item, index) in tabPaneList" :key="index" :tab="item.tab">
                    <a-row>
                        <a-col v-for="(i, idx) in item.form" :key="idx + 'b'" :span="12">
                            <a-form-item :label="i.label" :name="i.name">
                                <a-input-search v-if="i.type === 'input' && i.name === 'secret'" v-model:value="i.value"
                                    @search="getBase">
                                    <template #enterButton>
                                        <a-button type="primary">生成</a-button>
                                    </template>
                                </a-input-search>

                                <a-input-search v-if="i.type === 'inputChoose'" v-model:value="i.value"
                                    enter-button="选择" @search="openStrategyTable" />

                                <a-input v-if="i.type === 'input' && i.name !== 'secret'" v-model:value="i.value" />
                                <a-radio-group v-if="i.type === 'radio'" v-model:value="i.value" button-style="solid">
                                    <a-radio-button v-for="(e, num) in i.options" :value="e.value">{{ e.label
                                        }}</a-radio-button>
                                </a-radio-group>
                                <a-select v-if="i.type === 'select'" v-model:value="i.value" :mode="i.mode || ''"
                                    @change="i.change($event, i.name)" :options="i.options"></a-select>
                                <a-textarea v-if="i.type === 'textarea'" v-model:value="i.value"
                                    :auto-size="{ minRows: 2, maxRows: 5 }" />
                                <a-switch v-if="i.type === 'switch'" v-model:checked="i.value" :checkedValue="1"
                                    :unCheckedValue="0">
                                    <template #checkedChildren><check-outlined /></template>
                                    <template #unCheckedChildren><close-outlined /></template>
                                </a-switch>
                                <UploadImg v-if="i.type === 'img'" v-model:value="i.value"></UploadImg>
                                <UploadFile v-if="i.type === 'upFile'" v-model:value="i.value" />
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-tab-pane>
            </a-tabs>
        </a-form>
    </a-modal>

    <adapterTables ref="adapterTablesRef" @change="adapterTableChange" />
</template>
<script setup>
import { ref, nextTick, reactive, toRaw, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import _ from 'lodash';
import UploadImg from '/@/components/file/img.vue';
import UploadFile from '/@/components/file/uploadFile.vue';
import adapterTables from '/@/components/tableSelection/adapterTables.vue';
import { applicationManagementApi } from '/@/api/system/applicationManagement-api';
import { setWith } from 'lodash';
const findList = ref([]); //分类下拉

// 基本信息form
const form1 = ref([
    { label: '编码', name: 'id', value: '', type: 'input', options: [], disabled: false },
    { label: '应用密钥', name: 'secret', value: '', type: 'input', options: [], disabled: false },
    { label: '应用名称', name: 'appName', value: '', type: 'input', options: [], disabled: false },
    {
        label: '常用',
        name: 'frequently',
        value: 'no',
        defaultValue: 'no',
        type: 'radio',
        options: [
            { value: 'yes', label: '是' },
            { value: 'no', label: '否' },
        ],
        disabled: false,
    },
    {
        label: '协议',
        name: 'protocol',
        value: '',
        change: secretchange,
        type: 'select',
        options: [
            { label: 'OAuth v2.0', value: 'OAuth v2.0' },
            { label: 'OAuth v2.1', value: 'OAuth v2.1' },
            { label: 'OpenID_Connect_v1.0', value: 'OpenID_Connect_v1.0' },
        ],
        disabled: false,
    },
    { label: '分类', name: 'category', value: 'none', type: 'select', change: secretchange, options: findList.value, disabled: false },
    { label: '登录地址', name: 'loginUrl', value: '', type: 'input', options: [], disabled: false },
    { label: '状态', name: 'status', value: 1, defaultValue: 1, type: 'switch', options: [], disabled: false },
    {
        label: '配置扩展属性',
        name: 'extendAttr',
        value: 0,
        defaultValue: 0,
        type: 'radio',
        options: [
            { value: "0", label: '否' },
            { value: "1", label: '是' },
        ],
        disabled: false,
    },
    { label: '图标', name: 'iconId', value: '', type: 'img', options: [], disabled: false },
]);
// 扩展信息
const form4 = ref([
    { label: '注销地址', name: 'logoutUrl', value: '', type: 'input', options: [], disabled: false },
    {
        label: '注销方式',
        name: 'logoutType',
        value: '0',
        defaultValue: '0',
        type: 'select',
        options: [
            { label: '无', value: '0' },
            { label: '后台', value: '1' },
            { label: '前台', value: '2' },
        ],
        disabled: false,
    },
    {
        label: '权限范围',
        name: 'visible',
        value: '0',
        defaultValue: '0',
        type: 'select',
        options: [
            { label: '隐藏', value: '0' },
            { label: '所有用户', value: '1' },
            { label: '内部用户', value: '2' },
            { label: '外部用户', value: '3' },
        ],
        disabled: false,
    },
    { label: '排序', name: 'sortIndex', value: 1, defaultValue: 1, type: 'input', options: [], disabled: false },
    { label: '供应商', name: 'vendor', value: '', type: 'input', options: [], disabled: false },
    { label: '供应商网址', name: 'vendorUrl', value: '', type: 'input', options: [], disabled: false },
    {
        label: '适配',
        name: 'isAdapter',
        value: '0',
        defaultValue: '0',
        type: 'select',
        options: [
            { label: '禁用', value: '0' },
            { label: '启用', value: '1' },
        ],
        disabled: false,
    },
    { label: '适配器', name: 'adapterName', value: '', type: 'inputChoose', options: [], disabled: false },
    {
        label: '资源管理',
        name: 'resourceMgt',
        value: 0,
        defaultValue: 0,
        type: 'radio',
        options: [
            { value: 0, label: '否' },
            { value: 1, label: '是' },
        ],
        disabled: false,
    },
    {
        label: '引导',
        name: 'inducer',
        value: '',
        type: 'select',
        options: [
            { label: 'IDP', value: 'IDP' },
            { label: 'SP', value: 'SP' },
        ],
        disabled: false,
    },
    { label: '描述', name: 'description', value: '', type: 'textarea', options: [], disabled: false },
]);

// OAuth 2.0配置
const form2 = ref([
    { label: '认证地址', name: 'registeredRedirectUris', value: '', type: 'textarea', options: [], disabled: false },
    {
        label: '授权方式',
        name: 'select_authorizedGrantTypes',
        value: ['authorization_code'],
        defaultValue: ['authorization_code'],
        mode: 'multiple',
        type: 'select',
        options: [
            { label: 'authorization_code', value: 'authorization_code' },
            { label: 'password', value: 'password' },
            { label: 'client_credentials', value: 'client_credentials' },
            { label: 'implicit', value: 'implicit' },
            { label: 'id_token', value: 'id_token' },
            { label: 'token', value: 'token' },
            { label: 'refresh_token', value: 'refresh_token' },
        ],
        disabled: false,
    },
    {
        label: '主题',
        name: 'subject',
        value: 'username',
        defaultValue: 'username',
        type: 'select',
        options: [
            { label: '登录账号', value: 'username' },
            { label: '员工编号', value: 'employeeNumber' },
            { label: '电子邮箱', value: 'email' },
            { label: '手机号码', value: 'mobile' },
            { label: '域账号', value: 'windowsaccount' },
            { label: '用户编码', value: 'userId' },
        ],
        disabled: false,
    },
    {
        label: '作用域',
        name: 'select_scope',
        value: ['read'],
        defaultValue: ['read'],
        type: 'select',
        mode: 'multiple',
        options: [
            { label: 'read', value: 'read' },
            { label: 'write', value: 'write' },
            { label: 'trust', value: 'trust' },
            { label: 'openid', value: 'openid' },
            { label: 'profile', value: 'profile' },
            { label: 'email', value: 'email' },
            { label: 'phone', value: 'phone' },
            { label: 'address', value: 'address' },
            { label: 'all', value: 'all' },
        ],
        disabled: false,
    },
    {
        label: '许可确认',
        name: 'approvalPrompt',
        value: 'auto',
        defaultValue: 'auto',
        type: 'radio',
        options: [
            { value: 'force', label: '强制' },
            { value: 'auto', label: '自动' },
        ],
        disabled: false,
    },
    { label: 'accessToken有效期', name: 'accessTokenValiditySeconds', value: 300, defaultValue: 300, type: 'input', options: [], disabled: false },
    {
        label: 'PKCE',
        name: 'pkce',
        value: 0,
        defaultValue: 0,
        type: 'radio',
        options: [
            { value: 1, label: '是' },
            { value: 0, label: '否' },
        ],
        disabled: false,
    },
    { label: 'refreshToken有效期', name: 'refreshTokenValiditySeconds', value: 300, defaultValue: 300, type: 'input', options: [], disabled: false },
]);
// OpenID Connect配置
const form3 = ref([
    { label: '签发人(lssuer)', name: 'issuer', value: '', type: 'input', options: [], disabled: false },
    { label: '受众(Audience)', name: 'audience', value: '', type: 'input', options: [], disabled: false },
    {
        label: '签名算法',
        name: 'signature',
        value: '',
        type: 'select',
        change: secretchange,
        options: [
            { label: 'NONE', value: 'NONE' },
            { label: 'RS256', value: 'RS256' },
            { label: 'RS384', value: 'RS384' },
            { label: 'RS512', value: 'RS512' },
            { label: 'RS256', value: 'RS256' },
            { label: 'RS384', value: 'RS384' },
            { label: 'RS512', value: 'RS512' },
        ],
        disabled: false,
    },
    {
        label: '用户接口类型',
        name: 'userInfoResponse',
        value: '',
        type: 'select',
        options: [
            { label: 'NORMAL', value: 'NORMAL' },
            { label: 'SIGNING', value: 'SIGNING' },
            { label: 'ENCRYPYION', value: 'ENCRYPYION' },
            { label: 'SIGNING_ENCRYPTION', value: 'SIGNING_ENCRYPTION' },
        ],
        disabled: false,
    },
    { label: '签名密钥', name: 'signatureKey', value: '', type: 'textarea', options: [], disabled: false },
    { label: '秘钥', name: 'algorithmKey', value: '', type: 'textarea', options: [], disabled: false },
    {
        label: '加密算法',
        name: 'algorithm',
        value: '',
        change: secretchange,
        type: 'select',
        options: [
            { label: 'NONE', value: 'NONE' },
            { label: 'RSA1_5', value: 'RSA1_5' },
            { label: 'RSA_OAEP', value: 'RSA_OAEP' },
            { label: 'RSA_OAEP_256', value: 'RSA_OAEP_256' },
            { label: 'A128KW', value: 'A128KW' },
            { label: 'A192KW', value: 'A192KW' },
            { label: 'A256KW', value: 'A256KW' },
            { label: 'A128GCMKW', value: 'A128GCMKW' },
            { label: 'A192GCMKW', value: 'A192GCMKW' },
            { label: 'A256GCMKW', value: 'A256GCMKW' },
        ],
        disabled: false,
    },
    {
        label: '加密方法',
        name: 'encryptionMethod',
        value: '',
        type: 'select',
        options: [
            { label: 'A128GCM', value: 'A128GCM' },
            { label: 'A192GCM', value: 'A192GCM' },
            { label: 'A256GCM', value: 'A256GCM' },
            { label: 'A128CBC-HS256', value: 'A128CBC-HS256' },
            { label: 'A192CBC-HS384', value: 'A192CBC-HS384' },
            { label: 'A256CBC-HS512', value: 'A256CBC-HS512' },
            { label: 'XC20P', value: 'XC20P' },
        ],
        disabled: false,
    },
]);
// SAML V2.0配置
const form5 = ref([
    { label: 'ACS Url', name: 'spAcsUrl', value: '', type: 'input', options: [], disabled: false },
    {
        label: 'Binding',
        name: 'binding',
        value: '',
        type: 'select',
        options: [
            { label: 'Redirect-Post', value: 'Redirect-Post' },
            { label: 'Post-Post', value: 'Post-Post' },
            { label: 'IdpInit-Post', value: 'IdpInit-Post' },
            { label: 'Redirect-PostSimpleSign', value: 'Redirect-PostSimpleSign' },
            { label: 'Post-PostSimpleSign', value: 'Post-PostSimpleSign' },
            { label: 'IdpInit-PostSimpleSign', value: 'IdpInit-PostSimpleSign' },
        ],
        change: secretchange,
        disabled: false,

    },
    { label: 'EntityId', name: 'entityId', value: '', type: 'input', options: [], disabled: false },
    { label: 'Audience', name: 'audience', value: '', type: 'input', options: [], disabled: false },
    { label: 'lssuer', name: 'issuer', value: '', type: 'input', options: [], disabled: false },
    {
        label: '签名算法',
        name: 'signature',
        value: '',
        type: 'select',
        options: [
            { label: 'RSAwithSHA1', value: 'RSAwithSHA1' },
            { label: 'RSAwithSHA256', value: 'RSAwithSHA256' },
            { label: 'RSAwithSHA384', value: 'RSAwithSHA384' },
            { label: 'RSAwithSHA512', value: 'RSAwithSHA512' },
            { label: 'RSAwithMD5', value: 'RSAwithMD5' },
            { label: 'RSAwithRIPEMD160', value: 'RSAwithRIPEMD160' },
            { label: 'DSAwithSHA1', value: 'DSAwithSHA1' },
            { label: 'ECDSAwithSHA1', value: 'ECDSAwithSHA1' },
            { label: 'ECDSAwithSHA256', value: 'ECDSAwithSHA256' },
            { label: 'ECDSAwithSHA384', value: 'ECDSAwithSHA384' },
            { label: 'ECDSAwithSHA512', value: 'ECDSAwithSHA512' },
            { label: 'HMAC-MD5', value: 'HMAC-MD5' },
            { label: 'HMAC-SHA1', value: 'HMAC-SHA1' },
            { label: 'HMAC-SHA256', value: 'HMAC-SHA256' },
            { label: 'HMAC-SHA384', value: 'HMAC-SHA384' },
            { label: 'HMAC-SHA512', value: 'HMAC-SHA512' },
            { label: 'HMAC-RIPEMD160', value: 'HMAC-RIPEMD160' },
        ],
        disabled: false,
    },
    {
        label: '摘要方法',
        name: 'digestMethod',
        value: '',
        type: 'select',
        options: [
            { label: 'MD5', value: 'MD5' },
            { label: 'SHA1', value: 'SHA1' },
            { label: 'SHA256', value: 'SHA256' },
            { label: 'SHA384', value: 'SHA384' },
            { label: 'SHA512', value: 'SHA512' },
            { label: 'RIPEMD-160', value: 'RIPEMD-160' },
        ],
        disabled: false,
    },
    {
        label: '加密',
        name: 'encrypted',
        value: '',
        type: 'select',
        options: [
            { label: '加密', value: 'yes' },
            { label: '不加密', value: 'no' },
        ],
        disabled: false,
    },
    { label: '有效期', name: 'validityInterval', value: '', type: 'input', options: [], disabled: false },
    {
        label: 'NameIIdFormat',
        name: 'nameidFormat',
        value: '',
        type: 'select',
        options: [
            { label: 'persistent', value: 'persistent' },
            { label: 'transient', value: 'transient' },
            { label: 'emailAddress', value: 'emailAddress' },
            { label: 'X509SubjectName', value: 'X509SubjectName' },
            { label: 'WindowsDomainQualifiedName', value: 'WindowsDomainQualifiedName' },
            { label: 'unspecified', value: 'unspecified' },
            { label: 'entity', value: 'entity' },
        ],
        disabled: false,
    },
    {
        label: 'NameId转换',
        name: 'nameIdConvert',
        value: '',
        type: 'select',
        options: [
            { label: '原始', value: '0' },
            { label: '大写', value: '1' },
            { label: '小写', value: '2' },
        ],
        disabled: false,
    },
    {
        label: '上传证书类型',
        name: 'fileType',
        value: '',
        type: 'select',
        options: [
            { label: '证书', value: '0' },
            { label: 'SAML元数据文件', value: '1' },
            { label: 'SAML元数据地址', value: '2' },
        ],
        disabled: false,
    },
    { label: 'SAML元数据', name: 'metaFile', value: '', type: 'upFile', options: [], disabled: false },
    { label: '证书颁发者', name: 'certIssuer', value: '', type: 'input', options: [], disabled: false },
    { label: '证书有效期', name: 'certExpiration', value: '', type: 'input', options: [], disabled: false },
    { label: '证书主题', name: 'certSubject', value: '', type: 'input', options: [], disabled: false },
]);

// CAS配置
const form6 = ref([
    { label: '服务', name: 'service', value: '', type: 'input', options: [], disabled: false },
    { label: '回调地址', name: 'callbackUrl', value: '', type: 'input', options: [], disabled: false },
    {
        label: '返回账号',
        name: 'casUser',
        value: '',
        type: 'select',
        options: [
            { label: '登录账号', value: 'username' },
            { label: '员工编号', value: 'employeeNumber' },
            { label: '电子邮箱', value: 'email' },
            { label: '手机号码', value: 'mobile' },
            { label: '域账号', value: 'windowsaccount' },
            { label: '用户编码', value: 'userId' },
        ],
        disabled: false,
    },
    { label: '过期时间', name: 'expires', value: '', type: 'input', options: [], disabled: false },
]);
// JWT配置
const form7 = ref([
    { label: '认证地址', name: 'redirectUri', value: '', type: 'input', options: [], disabled: false },
    {
        label: '主题(Subject)',
        name: 'subject',
        value: 'username',
        type: 'select',
        options: [
            { label: '登录账号', value: 'username' },
            { label: '员工编号', value: 'employeeNumber' },
            { label: '电子邮箱', value: 'email' },
            { label: '手机号码', value: 'mobile' },
            { label: '域账号', value: 'windowsaccount' },
            { label: '用户编码', value: 'userId' },
        ],
        disabled: false,
    },
    {
        label: '令牌类型',
        name: 'tokenType',
        value: 'POST',
        type: 'select',
        options: [
            { label: 'GET', value: 'GET' },
            { label: 'POST', value: 'POST' },
            { label: 'LTPA', value: 'LTPA' },
        ],
        disabled: false,
    },
    { label: '名称', name: 'jwtName', value: 'jwt', type: 'input', options: [], disabled: false },
    { label: '过期时间', name: 'expires', value: '300', type: 'input', options: [], disabled: false },
    { label: '签发人(lssuer)', name: 'issuer', value: '', type: 'input', options: [], disabled: false },
    { label: '受众(Audience)', name: 'audience', value: '', type: 'input', options: [], disabled: false },
    {
        label: '签名算法',
        name: 'signature',
        value: '',
        type: 'select',
        options: [
            { label: 'NONE', value: 'NONE' },
            { label: 'RS256', value: 'RS256' },
            { label: 'RS384', value: 'RS384' },
            { label: 'RS512', value: 'RS512' },
            { label: 'RS256', value: 'RS256' },
            { label: 'RS384', value: 'RS384' },
            { label: 'RS512', value: 'RS512' },
        ],
        disabled: false,
    },
    { label: '签名密钥', name: 'signatureKey', value: '', type: 'textarea', options: [], disabled: false },
    {
        label: '加密算法',
        name: 'algorithm',
        value: '',
        type: 'select',
        options: [
            { label: 'NONE', value: 'NONE' },
            { label: 'RSA1_5', value: 'RSA1_5' },
            { label: 'RSA_OAEP', value: 'RSA_OAEP' },
            { label: 'RSA_OAEP_256', value: 'RSA_OAEP_256' },
            { label: 'A128KW', value: 'A128KW' },
            { label: 'A192KW', value: 'A192KW' },
            { label: 'A256KW', value: 'A256KW' },
            { label: 'A128GCMKW', value: 'A128GCMKW' },
            { label: 'A192GCMKW', value: 'A192GCMKW' },
            { label: 'A256GCMKW', value: 'A256GCMKW' },
        ],
        disabled: false,
    },
    {
        label: '加密方法',
        name: 'encryptionMethod',
        value: '',
        type: 'select',
        options: [
            { label: 'A128GCM', value: 'A128GCM' },
            { label: 'A192GCM', value: 'A192GCM' },
            { label: 'A256GCM', value: 'A256GCM' },
            { label: 'A128CBC-HS256', value: 'A128CBC-HS256' },
            { label: 'A192CBC-HS384', value: 'A192CBC-HS384' },
            { label: 'A256CBC-HS512', value: 'A256CBC-HS512' },
            { label: 'XC20P', value: 'XC20P' },
        ],
        disabled: false,
    },
    { label: '秘钥', name: 'algorithmKey', value: '', type: 'textarea', options: [], disabled: false },
]);
// 令牌配置
const form8 = ref([
    { label: '认证地址', name: 'redirectUri', value: '', type: 'input', options: [], disabled: false },
    {
        label: '令牌类型',
        name: 'tokenType',
        value: 'POST',
        type: 'select',
        options: [
            { label: 'GET', value: 'GET' },
            { label: 'POST', value: 'POST' },
            { label: 'LTPA', value: 'LTPA' },
        ],
        disabled: false,
    },
    { label: 'Cookie名称', name: 'cookieName', value: 'ltpa_token', type: 'input', options: [], disabled: false },
    {
        label: '加密算法',
        name: 'algorithm',
        value: 'AES',
        type: 'select',
        options: [
            { label: 'DES', value: 'DES' },
            { label: 'DESeda', value: 'DESeda' },
            { label: 'Blowfish', value: 'Blowfish' },
            { label: 'AES', value: 'AES' },
        ],
        disabled: false,
    },
    { label: '过期时间', name: 'expires', value: '300', type: 'input', options: [], disabled: false },
    { label: '令牌内容', name: 'content', value: '', type: 'input', options: [], disabled: false },
]);
// 表单配置
const form9 = ref([
    { label: '认证地址', name: 'redirectUri', value: '', type: 'input', options: [], disabled: false },
    { label: '登录名映射', name: 'usernameMapping', value: 'username', type: 'input', options: [], disabled: false },
    { label: '登录凭证映射', name: 'passwordMapping', value: 'password', type: 'input', options: [], disabled: false },
    { label: '认证视图', name: 'authorizeView', value: '', type: 'input', options: [], disabled: false },
    {
        label: '密码算法',
        name: 'passwordAlgorithm',
        value: 'NONE',
        type: 'select',
        options: [
            { label: 'NONE', value: 'NONE' },
            { label: 'MD5', value: 'MD5' },
            { label: 'SHA', value: 'SHA' },
            { label: 'SHA-1', value: 'SHA-1' },
            { label: 'SHA-256', value: 'SHA-256' },
            { label: 'SHA-384', value: 'SHA-384' },
            { label: 'SHA-512', value: 'SHA-512' },
            { label: 'MD5-HEX', value: 'MD5-HEX' },
            { label: 'SHA-HEX', value: 'SHA-HEX' },
            { label: 'SHA-1-HEX', value: 'SHA-1-HEX' },
            { label: 'SHA-256-HEX', value: 'SHA-256-HEX' },
            { label: 'SHA-384-HEX', value: 'SHA-384-HEX' },
            { label: 'SHA-512-HEX', value: 'SHA-512-HEX' },
        ],
        disabled: false,
    },
    { label: '凭证类型', name: 'credential', value: '', type: 'radio', options: [], disabled: false },
]);
// API配置
const form10 = ref([
    { label: '凭证', name: 'principal', value: '', type: 'input', options: [], disabled: false },
    { label: '秘钥', name: 'credentials', value: '', type: 'input', options: [], disabled: false },
    {
        label: '凭证类型', name: 'credential', value: '', type: 'radio', options: [{ label: '用户自定义', value: '0' },
        { label: '应用共享', value: '1' },
        { label: '系统配置', value: '2' },], disabled: false
    },
]);
const tabPaneList1 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: 'OAuth 2.0配置', form: form2.value },
    { tab: 'OpenID Connect配置', form: form3.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList2 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: 'SAML V2.0配置', form: form5.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList3 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: 'CAS配置', form: form6.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList4 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: 'JWT配置', form: form7.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList5 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: '令牌配置', form: form8.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList6 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: '表单配置', form: form9.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList7 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: 'API配置', form: form10.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList8 = ref([
    { tab: '基本信息', form: form1.value },
    { tab: '扩展信息', form: form4.value },
]);
const tabPaneList = ref(tabPaneList7.value);

const activeKey = ref(0);
// emit
const emit = defineEmits(['reloadList']);

// 组件ref
const formRef = ref();

const formDefault = {
    id: undefined,
};
let form = reactive({ ...formDefault });
const rules = {
    carCode: [{ required: true, message: '请输入车辆识别码' }],
    // carPlate: [{ required: true, message: '请输入车牌' }],
    cmpName: [{ required: true, message: '请输入公司名称' }],
    tag: [{ required: true, message: '请输入通行识别码' }],
    carType: [{ required: true, message: '请选择车辆类型' }],
    start: [{ required: true, message: '请选择时间' }],
    end: [{ required: true, message: '请选择时间' }],
};
// 是否展示抽屉
const visible = ref(false);
const formType = ref(null);
const modalType = ref();
async function showDrawer(rowData, index, type) {
    activeKey.value = 0;
    formType.value = index;
    modalType.value = type;
    if (type === '新增') {
        init();
    } else {
        tabPaneList.value = tabPaneList7.value;

        let resEdit;
        resEdit = await applicationManagementApi.initUrlGet('/extendapi', rowData.id);

        Object.assign(form, rowData);
        Object.assign(form, resEdit.data);

        tabPaneList.value.forEach((element) => {
            element.form.forEach((i) => {
                if (form[i.name]) {
                    i.value = form[i.name];
                }
            });
        });
    }
    visible.value = true;

}

const adapterTablesRef = ref(null);
function openStrategyTable() {
    adapterTablesRef.value.showModal();
}

//选择适配器回显数据 999
function adapterTableChange(data) {
    tabPaneList.value.forEach((element) => {
        element.form.forEach((i) => {
            if (i.name == 'adapterName') {
                i.value = data[0].name;
            }
            if (i.name == 'adapterId') {
                i.value = data[0].id;
            }
            form['adapterId'] = data[0].id;
        });
    });
}

function onClose() {
    Object.assign(form, formDefault);
    tabPaneList.value.forEach((element) => {
        element.form.forEach((i) => {
            i.value = i.defaultValue || '';
        });
    });
    visible.value = false;
}
async function onSubmit() {
    let paramObj = {};

    Object.assign(paramObj, form);
    tabPaneList.value.forEach((element) => {
        element.form.forEach((i) => {
            if (i.name === 'select_authorizedGrantTypes' && i.value && i.value.length) {
                paramObj['authorizedGrantTypes'] = i.value.join(',');
            } else if (i.name === 'select_scope' && i.value && i.value.length) {
                paramObj['scope'] = i.value.join(',');
            } else {
                paramObj[i.name] = i.value;
            }
        });
    });

    paramObj['switch_status'] = 0;

    let res;
    if (modalType.value == '编辑') {
        res = await applicationManagementApi.updateExtendapi(paramObj);
        message.success(`修改成功`);
    } else {
        res = await applicationManagementApi.addExtendapi(paramObj);
        message.success(`添加成功`);
    }

    onClose();
    emit('reloadList');
    emit('childEvent');
}
// 获取基本信息分类下拉
function getFindList() {
    applicationManagementApi.findList().then((res) => {
        if (res.code === 0) {
            findList.value = res.data.map((item) => {
                return { label: item.name, value: item.id };
            });
            findList.value.unshift({ label: 'ALL', value: '' });
            form1.value[5].options = findList.value;
        }
    });
}
// 获取应用密钥
function getBase() {
    applicationManagementApi.base().then((res) => {
        if (res.code === 0) {
            form1.value[1].value = res.data;
        }
    });
}
// 获取初始化数据
async function init() {
    let res = await applicationManagementApi.initExtendapi();
    if (formType.value) {
        Object.assign(form, res.data);
        tabPaneList.value.forEach((element) => {
            element.form.forEach((i) => {
                if (res.data[i.name]) {
                    i.value = res.data[i.name];
                }
            });
        });
    }
}
// 根据算法获取密钥
async function getSecret(url) {
    let res = await applicationManagementApi.secret({ id: form1.value[0].value }, url);
    if (res.code === 0) {
        return JSON.stringify(res.data);
    }
}
async function secretchange(value, type) {
    let res = await applicationManagementApi.secret({ id: form1.value[0].value }, value);
    if (res.code === 0) {
        if (type === 'signature') {
            form3.value[4].value = JSON.stringify(res.data);
        } else if (type === 'algorithm') {
            form3.value[7].value = JSON.stringify(res.data);
        }
    }
}
onMounted(() => {
    getFindList();
    init();
});
defineExpose({
    showDrawer,
});
</script>